﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using TransformerManager.Shared.Helper.HttpParams;
using TransformerManager.WPF.Common.Helper;
using hc = HandyControl.Controls;

namespace TransformerManager.WPF.Common.Http.Service
{
    public class UserInfoService : IUserInfoService
    {
        private readonly HttpRequest _httpRequest;
        public UserInfoService(HttpRequest httpRequest)
        {
            _httpRequest = httpRequest;
        }

        public async Task<UserBasicParams> GetUserInfoByIdAsync(string id)
        {
            string url = $"Account/userId/{id}";
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Get, url, null);
            string content = await responseMsg.Content.ReadAsStringAsync();
            ServiceResponse<UserBasicParams> result = 
                JsonConvert.DeserializeObject<ServiceResponse<UserBasicParams>>(content);
            if (!result.Success)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return result.Data;
            }
            return result.Data;
        }

        public async Task<UserBasicParams> GetUserInfoByUserNameAsync(string userName)
        {
            string url = $"Account/username/{userName}";
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Get, url, null);
            string content = await responseMsg.Content.ReadAsStringAsync();
            ServiceResponse<UserBasicParams> result = 
                JsonConvert.DeserializeObject<ServiceResponse<UserBasicParams>>(content);
            if (!result.Success)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return result.Data;
            }
            return result.Data;
        }

        public async Task<HeaderResponse<List<UserBasicParams>>> GetUsersAsync(string keywords, bool onlyLeadEngineer, int pageIndex, int pageSize)
        {
            string url = $"Account/users?keywords={keywords}&onlyLeadEngineer={onlyLeadEngineer}&PageNumber={pageIndex}&PageSize={pageSize}";
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Get, url, null);
            string content = await responseMsg.Content.ReadAsStringAsync();
            var headers = responseMsg.Headers;
            ServiceResponse<List<UserBasicParams>> result = 
                JsonConvert.DeserializeObject<ServiceResponse<List<UserBasicParams>>>(content);
            if (!result.Success)
            {
                hc.Growl.Error(
                        $"响应信息: {result.Message}",
                        "Notification");
                return new HeaderResponse<List<UserBasicParams>>
                {
                    Data = new List<UserBasicParams>(),
                    Headers = headers
                };
            }
            return new HeaderResponse<List<UserBasicParams>>
            {
                Data = result.Data,
                Headers = headers
            };
        }

        public async Task<ServiceResponse> ModifyPasswordAsync(ModifyPasswordParams modifyPasswordParams)
        {
            string url = $"Account/pwdchange";
            string reqBody = JsonConvert.SerializeObject(modifyPasswordParams);
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Post, url, null, reqBody);
            
            ServiceResponse result = new ServiceResponse();
            string content = await responseMsg.Content.ReadAsStringAsync();
            if (string.IsNullOrEmpty(content))
            {
                result.Success = false;
                result.Message = StatusCodeDescription.GetDescription(responseMsg.StatusCode);
                return result;
            }
            result = JsonConvert.DeserializeObject<ServiceResponse>(content);
            return result;
        }

        public async Task<ServiceResponse> ModifyUserInfoAsync(UserBasicParams userBasicParams)
        {
            string url = $"Account/userinfo";
            string reqBody = JsonConvert.SerializeObject(new
            {
                userBasicParams.UserId,
                userBasicParams.UserName,
                userBasicParams.RealNameCn,
                userBasicParams.RealNameEn,
                userBasicParams.Department,
                userBasicParams.IsLeadEngineer,
            });
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Post, url, null, reqBody);
            
            ServiceResponse result = new ServiceResponse();
            string content = await responseMsg.Content.ReadAsStringAsync();
            if (string.IsNullOrEmpty(content))
            {
                result.Success = false;
                result.Message = StatusCodeDescription.GetDescription(responseMsg.StatusCode);
                return result;
            }
            result = JsonConvert.DeserializeObject<ServiceResponse>(content);

            return result;
        }

        public async Task<ServiceResponse> ResetUserPasswordAsync(string userName)
        {
            string url = $"Account/pwdreset";
            string reqBody = JsonConvert.SerializeObject(
                new ResetPwdParams 
                    {
                        UserName = userName,
                        ResetPassword = "123456"
                    });
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Post, url, null, reqBody);
            
            ServiceResponse result = new ServiceResponse();
            string content = await responseMsg.Content.ReadAsStringAsync();
            if (string.IsNullOrEmpty(content))
            {
                result.Success = false;
                result.Message = StatusCodeDescription.GetDescription(responseMsg.StatusCode);
                return result;
            }
            result = JsonConvert.DeserializeObject<ServiceResponse>(content);

            return result;
        }

        public async Task<ServiceResponse> AuthorizeRole(string userName)
        {
            string url = "Account/authorize";
            var authorizeInfo = new
            {
                UserName = userName,
                RoleName = "Admin"
            };
            string reqBody = JsonConvert.SerializeObject(authorizeInfo);
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Post, url, null, reqBody);

            ServiceResponse result = new ServiceResponse();
            string content = await responseMsg.Content.ReadAsStringAsync();
            if(string.IsNullOrEmpty(content))
            {
                result.Success = false;
                result.Message = StatusCodeDescription.GetDescription(responseMsg.StatusCode);
                return result;
            }
            result = JsonConvert.DeserializeObject<ServiceResponse>(content);

            return result;
        }

        public async Task<ServiceResponse> UnauthorizeRole(string userName)
        {
            string url = "Account/unauthorize";
            var unauthorizeInfo = new
            {
                UserName = userName,
                RoleName = "Admin"
            };
            string reqBody = JsonConvert.SerializeObject(unauthorizeInfo);
            HttpResponseMessage responseMsg = await _httpRequest.RequestAsync(HttpMethod.Post, url, null, reqBody);

            ServiceResponse result = new ServiceResponse();
            string content = await responseMsg.Content.ReadAsStringAsync();
            if (string.IsNullOrEmpty(content))
            {
                result.Success = false;
                result.Message = StatusCodeDescription.GetDescription(responseMsg.StatusCode);
                return result;
            }
            result = JsonConvert.DeserializeObject<ServiceResponse>(content);

            return result;
        }
    }
}